<?php

namespace app\home\controller;

use app\company\service\DataService;
use library\Controller;
use think\Db;
use app\common\controller\Email;
use app\common\controller\Get;
use app\common\controller\Common;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;

/**
 * 物资盘点
 * Class User
 * @package app\worker\controller
 */
class WhInventory extends Controller
{

    protected $table = 'product_inventory';
    /**
     * 物资盘点
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '物资盘点';
        $this->ware_house_id = request()->get('ware_house_id');
        $query = $this->_query($this->table)
            ->dateBetween('create_at,time')
            ->where(['is_deleted' => '0'])
            ->where('ware_house_id', $this->ware_house_id)
            ->like('title')
            ->equal('status')
            ->order('create_at desc, id desc');
        $query->page();
    }

    protected function _index_page_filter(&$data)
    {
    }

    /**
     * 添加物资盘点
     * @auth true
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }


    /**
     * 修改物资盘点
     * @auth true
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 权限表单数据处理
     * @param array $data 表单数据
     */
    protected function _form_filter(&$data)
    {
        if (request()->isPost()) {
            $diff_price = 0;
            foreach($data['content']['product_id'] as $k=>$v){
                $content[] = [
                    'product_id' => $data['content']['product_id'][$k],
                    'name' => $data['content']['name'][$k],
                    'sku' => $data['content']['sku'][$k],
                    'price' => $data['content']['price'][$k],
                    'count' => $data['content']['count'][$k],
                    'real_count' => $data['content']['real_count'][$k],
                    'diff_price' => $data['content']['diff_price'][$k],
                ];
                $diff_price += ( $data['content']['real_count'][$k] - $data['content']['count'][$k] ) * $data['content']['price'][$k];
            }
            $data['content'] = json_encode($content);
            //计算差价
            $data['diff_price'] = $diff_price;
        }else{
            $products = Db::name('product_warehouse')->alias('pw')
                ->join('product p','p.id=pw.product_id','left')
                ->where('pw.ware_house_id', request()->get('ware_house_id'))
                ->field('pw.count,pw.product_id,p.*')
                ->order('pw.create_at desc,pw.id desc')
                ->select();
            $data['content'] = !empty($data['content']) ? json_decode($data['content'], true) : $products;
        }
    }

    /**
     * 签收
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function state()
    {
        $res = Db::name($this->table)->where('id', request()->post('id'))->find();
        $content = json_decode($res['content'], true);
        foreach($content as $v){
            Db::name('product_warehouse')
            ->where('ware_house_id', $res['ware_house_id'])
            ->where('product_id', $v['product_id'])
            ->update(['count' => $v['real_count']]);
        }
        Db::name($this->table)->where('id', request()->post('id'))->update(['status' => request()->post('status')]);
        $this->success('签收成功');
    }

    /**
     * 删除物资盘点
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

    public function content()
    {
        $this->content = json_decode(request()->get('content'), true);
        $this->fetch();
    }
}
